package com.bjpowernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjpowernode.entity.PmsAttr;
import com.bjpowernode.mapper.PmsAttrMapper;
import com.bjpowernode.mapper.PmsCategoryMapper;
import com.bjpowernode.service.PmsAttrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 分类属性表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2024-06-19
 */
@Service
public class PmsAttrServiceImpl extends ServiceImpl<PmsAttrMapper, PmsAttr> implements PmsAttrService {
    @Resource
    PmsCategoryMapper pmsCategoryMapper;

    //根据categoryIds来查询属性列表
    private List<PmsAttr> getByCategoryId(String categoryId) {
        QueryWrapper<PmsAttr> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId).orderByAsc("sort");
        return this.list(wrapper);
    }

    //根据categoryIds来查询属性列表，将前端传来的id数组遍历根据具体的id查询属性列表，再将属性列表添加到list中
    @Override
    public List<PmsAttr> list(String[] categoryIds) {
        List<PmsAttr> list = new ArrayList<>();
        for (String categoryId : categoryIds){
            List<PmsAttr> pmsAttrList = this.getByCategoryId(categoryId);
            //通过此方法将多个层级的id或者不同id的属性进行拼接到一个集合中
            list.addAll(pmsAttrList);
        }
        return list;
    }

    @Override
    public Boolean save(String name, String categoryId, Integer type) {
        return this.save(new PmsAttr(name, categoryId, type));
    }

    @Override
    public Boolean update(String id, String name, Integer type) {
        PmsAttr pmsAttr = new PmsAttr(name, type);
        pmsAttr.setId(id);
        return this.updateById(pmsAttr);
    }

    @Override
    public Boolean delete(String id) {
        return removeById(id);
    }
}
